package com.gt.xmall.service;

import java.util.List;
import java.util.Map;

import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

 
import com.gt.xmall.model.SysResource;
import com.gt.xmall.repository.SysResourceDao;
import com.gt.xmall.repository.SysResourceDaoEx;

@Component
@Transactional(readOnly = true)
public class SysResourceService {
	@Autowired
	SysResourceDao sysResourceDao;
	@Autowired
	SysResourceDaoEx sysResourceDaoEx;
	public List<SysResource> getAllSysResource(){
		
		return sysResourceDao.findAll();
	}
	
	
	public List getGrantedAuthorityResource(String resType){
		
		return sysResourceDao.findByResTypeOrderBySortNoAsc(resType);
	}
	
	public MenuRepository getMenuRepositoryByUserCode(String userCode,
			LinkedMap displayers) {
		// TODO Auto-generated method stub
//		Map menuMap = new HashMap();
		MenuRepository repository = new MenuRepository();
		repository.setDisplayers(displayers);
		List menus = sysResourceDaoEx.getMenusByUserCode(userCode);
		for(int i=0;i<menus.size();i++){
			MenuComponent mc =new MenuComponent();
//			mc.setName((String) ((Map)menus.get(i)).get("RES_NAME"));
			mc.setName(((Long) ((Map)menus.get(i)).get("RES_ID")).toString());
			mc.setTitle((String) ((Map)menus.get(i)).get("RES_TITLE"));
			mc.setUrl((String) ((Map)menus.get(i)).get("RES_URL"));
			mc.setLocation((String) ((Map)menus.get(i)).get("RES_URL"));
			if((Long) ((Map)menus.get(i)).get("PARENT_ID") != 0){
				mc.setParent(repository.getMenu(((Long) ((Map)menus.get(i)).get("PARENT_ID")).toString()));
			}
			repository.addMenu(mc);
			
		}
		return repository;
	}
}
